/**
 * Category.java 7 avr. 2006
 * 
 * Copyright (c) 2006 Emmanuel PUYBARET / eTeks <info@eteks.com>. All Rights
 * Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.windu2b.jcaddie.model;

import java.io.*;
import java.text.*;
import java.util.*;

import org.windu2b.jcaddie.swing.*;

/**
 * Category of product.
 * 
 * @author Emmanuel Puybaret, windu.2b
 */
public class Category implements Comparable<Category>, Serializable
{
	/**
	 * 
	 */
	private static final long	        serialVersionUID	= -6199614498386149651L;


	private List<CatalogPieceOfProduct>	listProducts;


	private String	                    name;


	private Content	                    icon;


	private static final Collator	    COMPARATOR	     = Collator
	                                                             .getInstance();


	private boolean	                    sorted;


	private String	                    id;




	/**
	 * Create a category.
	 * 
	 * @param name
	 *            the name of the cateory.
	 */
	public Category( String id, String name )
	{
		this( id, name, null );
	}




	public Category( String id, String name, Content icon )
	{
		this.name = name;
		this.id = id;
		this.icon = icon;
		if( icon == null )
			this.icon = IconManager.getInstance().getDefaultCategoryIcon();
		else
			this.icon = icon;
		listProducts = new ArrayList<CatalogPieceOfProduct>();
		COMPARATOR.setStrength( Collator.PRIMARY );
	}




	/**
	 * Returns true if this category and the one in parameter have the same
	 * name.
	 */
	@Override
	public boolean equals( Object obj )
	{
		return obj instanceof Category
		        && COMPARATOR.equals( name, ( ( Category ) obj ).name );
	}




	@Override
	public int hashCode()
	{
		return name.hashCode();
	}




	/**
	 * Compares the names of this category and the one in parameter.
	 */
	public int compareTo( Category category )
	{
		return COMPARATOR.compare( name, category.name );
	}




	/**
	 * Returns the product list of this category sorted by name.
	 * 
	 * @return an unmodifiable list of product.
	 */
	public List<CatalogPieceOfProduct> getProduct()
	{
		if ( !sorted )
		{
			Collections.sort( listProducts );
			sorted = true;
		}

		return Collections.unmodifiableList( listProducts );
	}




	/**
	 * @param listProducts
	 *            La nouvelle liste des produits pour cette catégorie 
	 */
	public void setListProducts( List<CatalogPieceOfProduct> listProducts )
	{
		this.listProducts = listProducts;
	}




	/**
	 * Adds a piece of product to this category.
	 * 
	 * @param piece
	 *            the piece to add.
	 * @throws IllegalArgumentException
	 *             if a piece with same name as the one in parameter already
	 *             exists in this category.
	 */
	void add( CatalogPieceOfProduct piece )
	{
		if ( listProducts.contains( piece ) )
		    throw new IllegalArgumentException( piece.getName()
		            + " already exists in " + name );
		listProducts.add( piece );

		sorted = false;
	}




	/**
	 * Returns Le nom de cette catégorie.
	 */
	public String getName()
	{
		return name;
	}




	/**
	 * @return L'icône associée à cette catégorie
	 */
	public Content getIcon()
	{
		return icon;
	}




	/**
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		String retour = "";
		for ( CatalogPieceOfProduct piece : listProducts )
		{
			retour += piece + "\t";
		}

		return retour;
	}




	/**
	 * @param icon Définit l'icône pour cette catégorie
	 */
	public void setIcon( Content icon )
	{
		this.icon = icon;
	}




	/**
	 * @return L'identifiant de la catégorie
	 */
	public String getId()
	{
		return id;
	}
}